package com.kuang.blog.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kuang.blog.entity.Emploee;
import com.kuang.blog.service.EmploeeSerive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张立帅
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/blog/emploee")
@Slf4j
public class EmploeeController {

    @Resource
    EmploeeSerive emploeeSerive;

    @ResponseBody
    @RequestMapping("/getEmploees")
    @CrossOrigin
    public String importPoSync(@RequestBody String object) throws JSONException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Emploee> emploees = emploeeSerive.querytAll();
        JSONArray list = new JSONArray();
        JSONObject map;
        for (Emploee emploee : emploees) {
            map = new JSONObject();
            if (emploee.getGmtCreate() == null) {
                map.put("date", "");
            } else {
                map.put("date", sdf.format(emploee.getGmtCreate()));
            }
            map.put("name", emploee.getName());
            map.put("address", emploee.getEmail());
            list.add(map);
        }
        JSONObject response = new JSONObject();
        response.put("type", "true");
        response.put("item", list);

        log.info(JSON.toJSONString(response));

        return JSON.toJSONString(response);
    }
}

